package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.MeetingRoomQueryDTO;
import com.sky.entity.MeetingRoom;
import com.sky.mapper.BookingOrderMapper;
import com.sky.mapper.MeetingRoomMapper;
import com.sky.result.PageResult;
import com.sky.service.MeetingRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MeetingRoomServiceImpl implements MeetingRoomService {

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;

    @Autowired
    private BookingOrderMapper bookingOrderMapper;

    @Override
    public List<MeetingRoom> findAll() {
        log.info("查询所有会议室信息");
        return meetingRoomMapper.selectList(null);
    }

    @Override
    public void update(MeetingRoom meetingRoom) {
        log.info("修改会议室参数：{}", meetingRoom);
        meetingRoomMapper.update(meetingRoom);
    }

    @Override
    public void deleteByid(Integer id) {
        log.info("删除会议室id："+id);
        meetingRoomMapper.delete(id);
    }

    @Override
    public void insert(MeetingRoom meetingRoom) {
        log.info("新增会议室参数：{}", meetingRoom);
        meetingRoomMapper.insert(meetingRoom);
    }

    /**
     * 筛选符合条件的会议室
     * @param query
     * @return
     */
    @Override
    public List<MeetingRoom> queryByConditions(MeetingRoomQueryDTO query) {
        if(query.getFloor()==0){
            query.setFloor(null);
        }

         List<MeetingRoom> meetingRoomList=meetingRoomMapper.queryByConditions(query);
         //在逻辑上  空闲、预定、使用都是可以的，因为后面还要根据时间来排除锁定和维护因为无法确认其使用时间，所以不行
         //ok上面是硬件条件合适的
        //下面要根据订单来查询这些会议室在客户要求的时间段内有没有已支付的订单，如果有那就剔除
        // 第二步：剔除在指定时间段内有已支付订单的会议室

        if (meetingRoomList.isEmpty()) {
            return meetingRoomList; // 如果没有符合条件的会议室，直接返回空列表
        }

        // 获取会议室ID列表
        List<Long> roomIds = meetingRoomList.stream()
                .map(MeetingRoom::getId)
                .collect(Collectors.toList());

        System.out.println(roomIds);

        // 查询在指定时间段内有已支付订单的会议室ID
        List<Long> bookedRoomIds = bookingOrderMapper.getPaidBookedRoomIds(
                query.getStartTime(),
                query.getEndTime(),
                roomIds
        );

        System.out.println(bookedRoomIds);

        // 从原始列表中移除在指定时间段内有已支付订单的会议室
        List<MeetingRoom> availableRooms = meetingRoomList.stream()
                .filter(room -> !bookedRoomIds.contains(room.getId()))
                .collect(Collectors.toList());

        //返回的都是符合的会议室
        return availableRooms;
    }

    /**
     * 分页查询会议室
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult page(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<MeetingRoom> meetingRooms = meetingRoomMapper.selectList(null);
        Page<MeetingRoom> page1=(Page<MeetingRoom>) meetingRooms;
        return new PageResult(page1.getTotal(),page1.getResult());
    }
}
